package ydy;

import org.weixin4j.Weixin;
import org.weixin4j.WeixinException;
import org.weixin4j.model.media.Article;
import org.weixin4j.model.media.Attachment;
import org.weixin4j.model.menu.Menu;
import org.weixin4j.model.menu.SingleButton;
import org.weixin4j.model.message.MediaType;
import org.weixin4j.model.user.Data;
import org.weixin4j.model.user.Followers;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ydy
 * @version 2018/5/6  17:12
 */
public class ArticleService {

    public String uploading() throws WeixinException {
        Weixin weixin = new Weixin();
        File file = new File("E:\\myimage2.jpg");
        String uploadimg = weixin.media().upload(MediaType.Image,file);
        return uploadimg;
    }

    /**
     * 获取mediaId 信息
     * @return
     * @throws WeixinException
     */
    public Attachment getImage() throws WeixinException {
        Weixin weixin = new Weixin();
        System.out.println(weixin.getAppId() + "appid");
        System.out.println(weixin.getSecret() + "secret");
        Attachment attachment = weixin.media().get("NFnb0wvaYZN2BCl-IYd-xMHxB8ErAX0GEZFXfMkIhC5RtpbLu9DQTz5z-9JWHgwO");
        return attachment;
    }

    /**
     * 上传图文信息
     * @return
     * @throws WeixinException
     */
    public String uploadingArticle() throws WeixinException {
        Weixin weixin = new Weixin();
        Article article = new Article();
        article.setTitle("测试素材"); //标题
        article.setThumb_media_id("NFnb0wvaYZN2BCl-IYd-xMHxB8ErAX0GEZFXfMkIhC5RtpbLu9DQTz5z-9JWHgwO");//图文消息的封面图片素材id（必须是永久mediaID）
        article.setAuthor("ydy"); //作者
        article.setDigest("测试摘要");//图文消息的摘要，仅有单图文消息才有摘要，多图文此处为空。如果本字段为没有填写，则默认抓取正文前64个字。
        article.setShow_cover_pic(1);//是否显示封面，0为false，即不显示，1为true，即显示
        article.setContent("测试内容");//图文消息的具体内容，支持HTML标签，必须少于2万字符，小于1M，且此处会去除JS,涉及图片url必须来源 "上传图文消息内的图片获取URL"接口获取。外部图片url将被过滤。
        article.setContent_source_url("https://www.baidu.com/"); //图文消息的原文地址，即点击“阅读原文”后的URL
        List<Article> articleList = new ArrayList<Article>();
        articleList.add(article);
        String uploadnews = weixin.media().uploadnews(articleList);
        return uploadnews;
    }

    /**
     * 发布图文
     * @return
     * @throws WeixinException
     */
    public String issueArticle() throws WeixinException {
        Weixin weixin = new Weixin();
        String[] openId = new String[]{"o6I-Sw4KJwbGW1tGkHmO9YK-uO6o","o6I-Sw5wDFTv00pKQEGp_8BesIz4"};
        return weixin.message().massSendNews(openId, "6sT88o8FiUKUkTsmczOdVEFDNGnDnqfNf07SBtBCNdloVrEwd4xm7uKeAWv6EyEz");
    }

    /**
     * 发布文本
     * @throws WeixinException
     */
    public void issueContext() throws WeixinException {
        Weixin weixin = new Weixin();
        weixin.message().customSendContent("o6I-Sw4KJwbGW1tGkHmO9YK-uO6o","发测试");
    }

    /**
     * 发布图片
     * @return
     * @throws WeixinException
     */
    public String issueImage() throws WeixinException {
        Weixin weixin = new Weixin();
        String[] openId = new String[]{"o6I-Sw4KJwbGW1tGkHmO9YK-uO6o","o6I-Sw5wDFTv00pKQEGp_8BesIz4"};
        return weixin.message().massSendNews(openId, "NFnb0wvaYZN2BCl-IYd-xMHxB8ErAX0GEZFXfMkIhC5RtpbLu9DQTz5z-9JWHgwO");
    }

    /**
     * 获取所有粉丝
     * @return
     */
    public Followers getUser() throws WeixinException {
        Weixin weixin = new Weixin("wxd3cf962e792de86a","d281ef553482060e908f20da0115937b");
        return weixin.user().getAll();
    }


    public void menuManagement() throws WeixinException {
        Weixin weixin = new Weixin();

//        //菜单二级按钮
//        List<SingleButton> buttonList2 = new ArrayList<SingleButton>();
//        SingleButton singleButton2 = new SingleButton();
//        singleButton2.setName("二级菜单1");
//        singleButton2.
//        buttonList2.add(singleButton2);

        //菜单一级按钮
        SingleButton singleButton1 = new SingleButton();
        singleButton1.setName("一级菜单");
//        singleButton1.setSubButton(buttonList2);

        List<SingleButton> button = new ArrayList<SingleButton>();
        button.add(singleButton1);
        Menu menu = new Menu();
        menu.setButton(button);
        weixin.menu().create(menu);
    }

    public static void main(String[] args) throws WeixinException {
        ArticleService articleService = new ArticleService();
//        String uploading = articleService.uploading();
//        System.out.println(uploading);


//        Attachment image = articleService.getImage();
//        System.out.println(image.getFileName());
//        System.out.println(image.getContentLength());
//        System.out.println(image.getError());
//        System.out.println(image.getContentType());
//        System.out.println(image.getFullName());
//        BufferedInputStream fileStream = image.getFileStream();
//        System.out.println(image.getFullName());


//        String s = articleService.uploadingArticle();
//        System.out.println(s);

//        String s = articleService.issueArticle();
//        System.out.println(s);

//        articleService.issueContext();

//        String s = articleService.issueImage();
//        System.out.println(s);


        Followers user = articleService.getUser();
        System.out.println(user.getCount());
        Data data = user.getData();
        List<String> openid = data.getOpenid();
        for (String s : openid) {
            System.out.println(s);
        }
        System.out.println(user.getNext_openid());
        System.out.println(user.getTotal());

//        articleService.menuManagement();

    }
}
